package com.bdqn.service.impl;

import com.bdqn.dao.IUserInfoDAO;
import com.bdqn.entity.UserInfo;
import com.bdqn.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class IUserInfoServiceImpl implements IUserInfoService {

    private IUserInfoDAO userInfoDao;

    public IUserInfoDAO getUserInfoDao() {
        return userInfoDao;
    }

    public void setUserInfoDao(IUserInfoDAO userInfoDao) {
        this.userInfoDao = userInfoDao;
    }

    public int addUserInfo(UserInfo info) {
        return userInfoDao.addUserInfo(info);
    }
    @Transactional
    public boolean buyUserInfo(UserInfo info) {
        //一共添加两个数据第一个添加完成后 测试事物
        int successNumber = userInfoDao.addUserInfo(info);
        if (successNumber == 0){
            throw new RuntimeException("这是第一个测试异常");
        }
        info.setUserName(info.getUserName() + "---01");
        //这个IF是模拟 业务逻辑出错
        if (1 == 1){
            throw new RuntimeException("这是第二个测试异常：业务逻辑异常");
        }
        int successNumberTwo = userInfoDao.addUserInfo(info);

        if (successNumber == 1 && successNumberTwo ==1){
            return true;
        }else{
            return false;
        }
    }
}
